/**
 * Copyright (C) 2012  The CFuture Android SDK Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.android.toolkit.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

/**
 * 
 * @author cfuture.chenyoca [桥下一粒砂] (chenyoca@163.com)
 * @date 2012-2-17
 */
public class NetworkUtil {

	public static final String CTWAP = "ctwap";
	public static final String CMWAP = "cmwap";
	public static final String WAP_3G = "3gwap";
	public static final String UNIWAP = "uniwap";
	
	/**
	 * 网络不可用。
	 */
	public static final int TYPE_NET_WORK_DISABLED = 0;// 网络不可用
	
	/**
	 * 移动联通wap10.0.0.172
	 */
	public static final int TYPE_CM_CU_WAP = 4;
	
	/**
	 * 电信wap 10.0.0.200
	 */
	public static final int TYPE_CT_WAP = 5;
	
	/**
	 * 电信,移动,联通,wifi 等net网络
	 */
	public static final int TYPE_OTHER_NET = 6;
	public static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

	/**
	 * 取得网络状态
	 * 
	 * @param context
	 * @return 如果网络（3G/wifi）可用，则返回true。否则返回false。
	 */
	public static boolean isNetworkActive(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = manager.getActiveNetworkInfo();
		return !(networkinfo == null || !networkinfo.isAvailable());
	}
//
//	/**
//	 * 判断Wifi是否开启
//	 * 
//	 * @param c
//	 * @return 开启则返回true，否则返回false。
//	 */
//	public static boolean isWifiActivate(Context c) {
//		return ((WifiManager) c.getSystemService(Context.WIFI_SERVICE))
//				.isWifiEnabled();
//	}
//
//	/**
//	 * 修改Wifi状态
//	 * 
//	 * @param c
//	 *            Activity上下文
//	 * @param status
//	 *            开启为true,关闭为false。
//	 */
//	public static void changeWifi(Context c, boolean status) {
//		((WifiManager) c.getSystemService(Context.WIFI_SERVICE))
//				.setWifiEnabled(status);
//	}
//
//	/***
//	 * 判断Network具体类型（联通移动wap，电信wap，其他net）
//	 * 
//	 * */
//	public static int checkNetworkType(Context mContext) {
//		try {
//			final ConnectivityManager connectivityManager = (ConnectivityManager) mContext
//					.getSystemService(Context.CONNECTIVITY_SERVICE);
//			final NetworkInfo mobNetInfoActivity = connectivityManager.getActiveNetworkInfo();
//			if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {
//				return TYPE_OTHER_NET;
//			} else {
//				// NetworkInfo不为null开始判断是网络类型
//				int netType = mobNetInfoActivity.getType();
//				if (netType == ConnectivityManager.TYPE_WIFI) {
//					return TYPE_OTHER_NET;
//				} else if (netType == ConnectivityManager.TYPE_MOBILE) {
//
//					final Cursor cursor = mContext.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
//					if (cursor != null) {
//						cursor.moveToFirst();
//						final String user = cursor.getString(cursor.getColumnIndex("user"));
//						if (!TextUtils.isEmpty(user) && user.startsWith(CTWAP)) {
//								return TYPE_CT_WAP;
//						}
//					}
//					cursor.close();
//
//					String netMode = mobNetInfoActivity.getExtraInfo();
//					if (netMode != null) {
//						// 通过apn名称判断是否是联通和移动wap
//						netMode = netMode.toLowerCase();
//						if (netMode.equals(CMWAP) || netMode.equals(WAP_3G)|| netMode.equals(UNIWAP)) {
//							return TYPE_CM_CU_WAP;
//						}
//					}
//				}
//			}
//		} catch (Exception ex) {
//			ex.printStackTrace();
//			return TYPE_OTHER_NET;
//		}
//
//		return TYPE_OTHER_NET;
//
//	}

}
